package com.ruoyi.web.controller.shopping;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.adapayBalanceRecord.domain.AdapayBalanceRecord;
import com.ruoyi.adapayBalanceRecord.service.IAdapayBalanceRecordService;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.store.domain.Store;
import com.ruoyi.store.service.IStoreService;
import com.ruoyi.technician.domain.StoreTechnician;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.shopping.domain.MallCommissionRecord;
import com.ruoyi.shopping.service.IMallCommissionRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 佣金记录Controller
 *
 * @author scx
 * @date 2022-06-10
 */
@RestController
@RequestMapping("/shopping/commissionRecord")
public class MallCommissionRecordController extends BaseController
{
    @Autowired
    private IMallCommissionRecordService mallCommissionRecordService;
    @Autowired
    private IAdapayBalanceRecordService adapayBalanceRecordService;
    @Autowired
    private IStoreService storeService;



    /**
     * 查询佣金记录列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:commissionRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdapayBalanceRecord adapayBalanceRecord)
    {
        startPage();
        adapayBalanceRecord.setHandleType(DicConstants.HANDLE_BALANCE_ADD.getCode());
        adapayBalanceRecord.setBusType(DicConstants.BUSTYPE_BALANCE_MALL_DIV.getCode());
        List<AdapayBalanceRecord> list = adapayBalanceRecordService.selectAdapayBalanceRecordList(adapayBalanceRecord);
        TableDataInfo getDataTable=getDataTable(list);
        if(list.size()>0){
            List<Long> storeIds= list.stream().map(AdapayBalanceRecord::getStoreId).distinct().collect(Collectors.toList());
            List<Store> stores=storeService.selectStoreListByIds(storeIds);
            Map<Long,Store> storeMap=stores.stream().collect(Collectors.toMap(Store::getId, Function.identity()));
            JSONArray jsonArray=new JSONArray();
            for (AdapayBalanceRecord balanceRecord : list) {
                JSONObject jso=new JSONObject();
                jso.put("money", Arith.numTostr(balanceRecord.getMoney()));
               String storeName="";
               String storeImage="";
               if(storeMap.containsKey(balanceRecord.getStoreId())){
                    storeName= storeMap.get(balanceRecord.getStoreId()).getStoreName();
                    storeImage= storeMap.get(balanceRecord.getStoreId()).getStoreImage();
                }
                jso.put("storeName",storeName);
                jso.put("createTime",balanceRecord.getCreateTime());
                jso.put("storeImage",storeImage);
                jsonArray.add(jso);
            }
            getDataTable.setRows(jsonArray);
        }



        return getDataTable;
    }

    /**
     * 导出佣金记录列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:commissionRecord:export')")
    @Log(title = "佣金记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallCommissionRecord mallCommissionRecord)
    {
        List<MallCommissionRecord> list = mallCommissionRecordService.selectMallCommissionRecordList(mallCommissionRecord);
        ExcelUtil<MallCommissionRecord> util = new ExcelUtil<MallCommissionRecord>(MallCommissionRecord.class);
        return util.exportExcel(list, "佣金记录数据");
    }

    /**
     * 获取佣金记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:commissionRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallCommissionRecordService.selectMallCommissionRecordById(id));
    }

    /**
     * 新增佣金记录
     */
    @PreAuthorize("@ss.hasPermi('shopping:commissionRecord:add')")
    @Log(title = "佣金记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallCommissionRecord mallCommissionRecord)
    {
        return toAjax(mallCommissionRecordService.insertMallCommissionRecord(mallCommissionRecord));
    }

    /**
     * 修改佣金记录
     */
    @PreAuthorize("@ss.hasPermi('shopping:commissionRecord:edit')")
    @Log(title = "佣金记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallCommissionRecord mallCommissionRecord)
    {
        mallCommissionRecord.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(mallCommissionRecordService.updateMallCommissionRecord(mallCommissionRecord));
    }

    /**
     * 删除佣金记录
     */
    @PreAuthorize("@ss.hasPermi('shopping:commissionRecord:remove')")
    @Log(title = "佣金记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallCommissionRecordService.logicDeleteMallCommissionRecord(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
